﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    protected override void OnPreLoad(EventArgs e)
    {
        base.OnPreLoad(e);
        if (!CheckPermission())
        {
            Common.GoNoPermissionPage();
        }

    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        
        if (CurrentUserInfo.RwType == 0)
        {
            SettingPageOnlyRead();
        }
    }

    public void SettingPageOnlyRead()
    {
        string needWPesmission = "needWPesmission";
        foreach (Control control in Page.Form.Controls)
        {
            if (control is WebControl)
            {
                if (((WebControl)control).Attributes[needWPesmission] != null)
                {
                    SetControlStatus(control,((WebControl)control).Attributes[needWPesmission]);
                    //control.Visible = false;
                }
                else
                {
                    if (control.HasControls())
                    {
                        SettingControlOnlyRead(control);
                    }
                }
            }
            else
            {
                if (control.HasControls())
                {
                    SettingControlOnlyRead(control);
                }
            }
        }
    }

    private void SetControlStatus(Control c,string status)
    {
        if (status=="Disable")
        {
            ((WebControl)c).Enabled = false;
        }
        else
        {
            c.Visible = false;
        }
    }

    private void SettingControlOnlyRead(Control control)
    {
        string needWPesmission = "needWPesmission";
        foreach (Control childControl in control.Controls)
        {
            if (childControl is WebControl)
            {
                if (((WebControl)childControl).Attributes[needWPesmission] != null)
                {
                    SetControlStatus(childControl, ((WebControl)childControl).Attributes[needWPesmission]);
                }
                else
                {
                    if (childControl.HasControls())
                    {
                        SettingControlOnlyRead(childControl);
                    }
                }
            }
            else
            {
                if (childControl.HasControls())
                {
                    SettingControlOnlyRead(childControl);
                }
            }
        }
    }


    protected virtual bool CheckPermission()
    {
        return true;
    }

    public PKURG.CostSplit.Entities.SysUser CurrentUserInfo
    {
        get
        {
            return new UserHelper().GetCurrentUserInfo();
        }
        // set { _CurrentUserInfo = value; }
    }



}